const config = require('./config.js')

function request(options) {
    if (!options.retryTimes && options.retryTimes !== 0) {
        options.retryTimes = 3;
    }
    if (options.url.indexOf('http') !== 0) {
        options.url = config.serverHost + options.url
    }
    /// ----
    if (options.url.indexOf('api/login.php') < 0) {
      if (options.method=='POST'){
        options.header = { 
          "Cookie": wx.getStorageSync('mockSessionCookies'), 
          'content-type': 'application/x-www-form-urlencoded' }
      }else{
        options.header = { "Cookie": wx.getStorageSync('mockSessionCookies') }
    }
    }
    let originSuccess = options.success
    let originFail = options.fail
    let success = function(res) {
        originSuccess(res)
    }
    let fail = function(err) {
        if (options.retryTimes > 0) {
            options.retryTimes--;
            request(options);
        } else {
            originFail || originFail(err)
        }
    }
    options.success = success
    options.fail = fail
    return wx.request(options)
}

function get(url, isOrigin) {
    return new Promise(function(resolve, reject) {
        request({
            url: url,
            method: 'GET',
            success: function(res) {
                if (isOrigin) {
                    resolve(res)
                } else {
                    resolve(res.data)
                }
            },
            fail: function(err) {
                reject(err)
            }
        })
    })
}

function post(url, data, isOrigin) {
    return new Promise(function(resolve, reject) {
        request({
            url: url,
            method: 'POST',
            data: data,
            success: function(res) {
                if (isOrigin) {
                    resolve(res)
                } else {
                    resolve(res.data)
                }
            },
            fail: function(err) {
                console.log("埋点错误")
                console.log(err)
                // reject(err)
            }
        })
    })
}

function del(url) {
    return new Promise(function(resolve, reject) {
        request({
            url: url,
            method: 'DELETE',
            success: function(res) {
                resolve(res.data)
            },
            fail: function(err) {
                reject(err)
            }
        })
    })
}

function put(url, data) {
    return new Promise(function(resolve, reject) {
        request({
            url: url,
            method: 'PUT',
            data: data,
            success: function(res) {
                resolve(res.data)
            },
            fail: function(err) {
                reject(err)
            }
        })
    })
}

/**
 * 数据埋点
 */
function dataBuried(bruyData) {
    wx.request({
      url: config.serverDataBury,
      header: {
        "Content-Type": "application/json",
      },
      method: 'POST',
      data: bruyData,
      success: function (res) {
        console.log('dataBuried success:',res)
      },
      fail: function (err) {
        // reject(err)
      }
    })
}

module.exports = {
    get,
    post,
    del,
    put,
    dataBuried
}